package cn.bdqn.crm.controller;

import cn.bdqn.crm.domain.dto.Dto;
import cn.bdqn.crm.domain.dto.DtoUtil;
import cn.bdqn.crm.domain.dto.Page;
import cn.bdqn.crm.domain.vo.BasDictVo;
import cn.bdqn.crm.service.BasDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/back/dict")
public class BasDictController {

    @Autowired
    private BasDictService basDictService;

    @RequestMapping(value = "/findByPage",method = RequestMethod.POST)
    public Dto<Page<BasDictVo>> findByPage(
            @RequestBody(required = false) Map<String,Object> mapCondition){
        System.out.println(mapCondition);
        if(mapCondition==null){
            mapCondition = new HashMap<>();
            mapCondition.put("pageNo",1);
            mapCondition.put("pageSize",5);
            mapCondition.put("dictType",null);
            mapCondition.put("dictItem",null);
            mapCondition.put("dictValue",null);
        }else{
            Integer pageNo = (Integer)mapCondition.get("pageNo");
            Integer pageSize = (Integer)mapCondition.get("pageSize");
            if(pageNo==null)
                pageNo = 1;
            if(pageSize==null)
                pageSize = 5;
            mapCondition.put("pageNo",pageNo);
            mapCondition.put("pageSize",pageSize);
        }
        System.out.println(mapCondition);
        try {
            Page<BasDictVo> pageBean = basDictService.findByPage(mapCondition);
            return DtoUtil.returnSuccess("成功",pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("后台查询失败!","500");
        }

    }
}
